package com.zhxy.interceptor;

import io.jsonwebtoken.Claims;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class JWTInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        /**
         * 前后端分离有时候会有两次请求，第一次为OPTIONS请求，默认会拦截所有请求。
         */
        if (HttpMethod.OPTIONS.toString().equals(request.getMethod())) {
            return true; //放行
        }
        System.out.println("经过了拦截器");

        String token = request.getHeader("Authorization");//token
        //System.out.println("Authorization:"+token);
        if(token==null){
            throw new Exception("无权访问");
        }
        StringBuffer requestURL = request.getRequestURL();
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        System.out.println(requestURL);



        //登录和注册不用拦截的


        return true;
    }
}
